package org.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.Prod;
import org.example.domain.Sku;
import org.example.model.ChangeStock;
import org.example.model.Result;
import org.example.service.ProdService;
import org.example.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品接口管理")
@RequestMapping("prod/prod")
@RestController
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;

    @ApiOperation("多条件分页查询商品")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProdPage(@RequestParam Long current,
                                           @RequestParam Long size,
                                           @RequestParam(required = false) String prodName,
                                           @RequestParam(required = false) Long status) {
        // 创建商品分页对象
        Page<Prod> page = new Page<>(current, size);
        // 多条件分页查询商品
        page = prodService.page(page,  new LambdaQueryWrapper<Prod>()
                .eq( ObjectUtil.isNotNull( status),  Prod::getStatus, status)
                .like(StringUtils.hasText( prodName),  Prod::getProdName,  prodName)
                .orderByDesc( Prod::getCreateTime));

        return Result.success( page);
    }

    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProd(@RequestBody Prod prod) {
        Boolean saved = prodService.saveProd(prod);
        return Result.handle( saved);
    }

    @ApiOperation("根据标识查询商品详情")
    @GetMapping("info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> loadProdInfo(@PathVariable Long prodId) {
        Prod prod = prodService.queryProdInfoById(prodId);
        return Result.success( prod);
    }

    @ApiOperation("修改商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> modifyProdInfo(@RequestBody Prod prod) {
        Boolean modified = prodService.modifyProdInfo(prod);
        return Result.handle( modified);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> removeProd(@PathVariable Long prodId) {
        Boolean removed = prodService.removeProdById(prodId);
        return Result.handle( removed);
    }

    // TODO: 2025/11/01 OpenFeign远程调用
    @ApiOperation("根据标识查询轮播图详情列表")
    @GetMapping("getProdListByIds")
    public Result<List<Prod>> getProdListByIds(@RequestParam List<Long> prodIdList) {
        List<Prod> prods = prodService.listByIds(prodIdList);
        return Result.success(prods);
    }

    @GetMapping("/getProdListByCategoryIds")
    public Result<List<Prod>> getProdListByCategoryIds(@RequestParam List<Long> categoryIds){
        List<Prod> list = prodService.list(new LambdaQueryWrapper<Prod>()
                .in( Prod::getCategoryId, categoryIds));
        return Result.success(list);
    }

    @GetMapping("getSkuListBySkuIds")
    public Result<List<Sku>> getSkuListBySkuIds(@RequestParam List<Long> skuIds){
        List<Sku> skus = skuService.listByIds(skuIds);
        return Result.success( skus);
    }

    @GetMapping("/getProdListByShopIds")
    public Result<List<Prod>> getProdListByProIds(@RequestParam List<Long> shopIds){
        List<Prod> prods = prodService.list(new LambdaQueryWrapper<Prod>()
                .in( Prod::getShopId, shopIds));
        return Result.success(prods);
    }

    @PostMapping("/changeProdAndSkuStock")
    public Result<Boolean> changeProdAndSkuStock(@RequestBody ChangeStock changeStock) {
        Boolean changed = prodService.changeProdAndSkuChangeStock(changeStock);
        return Result.success( changed);
    }

    @GetMapping("/getSeckillProdByProdIds")
    public Result<List<Prod>> getSeckillProdByProdIds(@RequestParam List<Long> prodIds){
        // 根据商品prodId集合查询商品对象集合
        List<Prod> prodList = prodService.listByIds(prodIds);
        return Result.success( prodList);
    }


    // TODO: 2025/11/05 小程序接口调用
    @ApiOperation("小程序根据商品标识查询商品详情")
    @GetMapping("/prod/prodInfo")
    public Result<Prod> loadWxProdInfo(@RequestParam Long prodId) {
        // 根据商品标识查询商品详情
        Prod prod = prodService.queryWxProdInfoByProdId(prodId);
        return Result.success( prod);
    }

    @ApiOperation("小程序根据商品标识查询商品详情")
    @GetMapping("/seckillProd/info")
    public Result<Prod> loadWxSeckillProdInfo(@RequestParam Long prodId, @RequestParam Long skuId) {
        // 根据商品标识查询商品详情
        Prod prod = prodService.queryWxSeckillProdInfoByProdId(prodId, skuId);
        return Result.success( prod);
    }

}
